﻿using System;

namespace CRM.Database.Model
{
    public partial class PriceConditions : IGetKeyValue, ISetChangedUser
    {
        #region Implementation of IGetKeyValue

        /// <summary>
        /// Получение ключего значения элемента
        /// </summary>
        /// <returns>Ключевое значение</returns>
        public int GetKeyValue()
        {
            return IdPriceCondition;
        }

        #endregion

        #region Implementation of ISetChangedUser

        /// <summary>
        /// Установка пользователя, который сделал изменение
        /// </summary>
        /// <param name="userLogin">Логин пользователя</param>
        public void SetChangedUser(string userLogin)
        {
            if (string.IsNullOrEmpty(userLogin))
                throw new NotImplementedException("Необходимо указать пользователя");
            IdUser = userLogin;
        }

        #endregion

        
        
        /// <summary>
        /// Продукт
        /// </summary>
        public string ProductStr
        {
            get { return Products.ArticleLong; }
        }


        public string LegalEntityName
        {
            get { if (LegalEntities != null) return LegalEntities.Name;
                return "-";
            }
        }

        /// <summary>
        /// Тип ценового условия
        /// </summary>
        public string PriceConditionTypeStr
        {
            get { return this.PriceConditionTypes.GetLocalizedName(); }
        }


        /// <summary>
        /// Транспортное условие
        /// </summary>
        public string TransportConditionStr
        {
            get { if (TransportConditions != null)
                if (TransportConditions.TrucksCapacities != null)
                    return "Вместимость "+ TransportConditions.TrucksCapacities.Capacity.ToString() + " паллетт, стоимость транспортировки 1 паллетты "+TransportConditions.PallettTransportationCostStr;
                return "-";
            }
        }


        /// <summary>
        /// Метров в паллетте
        /// </summary>
        public double? MetresInPallette
        {
            get { if (Products != null) return ((Products.ProfileLength * Products.StabProPallette) / 1000);
                return 0;
            }
        }



        /// <summary>
        /// Метров в паллетте
        /// </summary>
        public string MetresInPalletteStr
        {
            get { return MetresInPallette.ToString(); }
        }


        /// <summary>
        /// Наценка
        /// </summary>
        public double ExtraCharge
        {
            get
            {
                if (TransportConditions != null)
                {
                    double? d = TransportConditions.PallettTransportationCost/MetresInPallette;
                    if (d != null)
                        return (double) d;
                }
                return 0;
            }
        }

        

        /// <summary>
        /// Наценка
        /// </summary>
        public string ExtraChargeStr
        {
            get { return ExtraCharge.ToString(); }
        }


        /// <summary>
        /// Цена с транспортом
        /// </summary>
        public double PriceWithTransport
        {
            get { return (double) (Price + (decimal) ExtraCharge); }
        }


        /// <summary>
        /// Цена с транспортом
        /// </summary>
        public string PriceWithTransportStr
        {
            get { return PriceWithTransport.ToString(); }
        }

        public string PriceCondition
        {
            get { return LegalEntityName + ", " + ProductStr; }
        }

        public string PriceConditionStr
        {
            get { return LegalEntityName+ ", "+ ProductStr + " " + Price+" евро за метр"; }
        }
    }
}